package com.example.testcourse;

import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.widget.Button;
import android.widget.LinearLayout;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {
    Button bn;
    LinearLayout weekPanels[]=new LinearLayout[7];
    List courseData[]=new ArrayList[7];
    int itemHeight;
    int marTop,marLeft;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bn = findViewById(R.id.bn_go);
        bn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,Alarm.class);
                startActivity(intent);
            }
        });
        itemHeight=getResources().getDimensionPixelSize(R.dimen.weekItemHeight);
        marTop=getResources().getDimensionPixelSize(R.dimen.weekItemMarTop);
        marLeft=getResources().getDimensionPixelSize(R.dimen.weekItemMarLeft);
        getData();
        for (int i = 0; i < weekPanels.length; i++) {
            weekPanels[i]=(LinearLayout) findViewById(R.id.weekPanel_1+i);
            initWeekPanel(weekPanels[i], courseData[i]);
        }
    }

    public void getData(){
        List<Course>list1=new ArrayList<Course>();
        list1.add(new Course("算法设计与分析", "C1-201", 2, 2, "张三", " "));
        list1.add(new Course("WEB程序设计", "D1-402", 4, 2, "李四", " "));
        courseData[0]=list1;

        List<Course>list2=new ArrayList<Course>();
        list2.add(new Course("Python数据分析与应用", "D5-203", 6, 3, "王五", " "));
        courseData[1]=list2;

        List<Course>list3=new ArrayList<Course>();
        list3.add(new Course("信息论与编码理论", "D5-103", 2, 2, "张三", " "));
        list3.add(new Course("计算机创新实践", "  ", 5, 3, "李四", " "));
        courseData[2]=list3;

        List<Course>list4=new ArrayList<Course>();
        list4.add(new Course("Android系统应用层软件设计", "C2-303", 3, 2, "王五", " "));
        list4.add(new Course("习近平新时代中国特色社会主义思想概论", "F5-101", 9, 3, "张三", " "));
        courseData[3]=list4;

        List<Course>list5=new ArrayList<Course>();
        list5.add(new Course("数字电路与逻辑设计","C1-203",5,3,"李四"," "));
        list5.add(new Course("随机数学","D4-103",9,3,"王五","备注：提早到教室"));
        courseData[4]=list5;
    }


    public void initWeekPanel(LinearLayout ll,List<Course>data){
        if(ll==null || data==null || data.size()<1)return;
        Log.i("Msg", "初始化面板");
        Course pre=data.get(0);
        for (int i = 0; i < data.size(); i++) {
            Course c =data.get(i);
            TextView tv =new TextView(this);
            LinearLayout.LayoutParams lp =new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.FILL_PARENT ,
                    itemHeight*c.getStep()+marTop*(c.getStep()-1));
            if(i>0){
                lp.setMargins(marLeft, (c.getStart()-(pre.getStart()+pre.getStep()))*(itemHeight+marTop)+marTop, 0, 0);
            }else{
                lp.setMargins(marLeft, (c.getStart()-1)*(itemHeight+marTop)+marTop, 0, 0);
            }

            tv.setLayoutParams(lp);
            tv.setGravity(Gravity.TOP);
            tv.setGravity(Gravity.CENTER_HORIZONTAL);
            tv.setTextSize(12);
            tv.setTextColor(getResources().getColor(R.color.courseTextColor));
            tv.setText(c.getName()+"\n"+c.getRoom()+"\n"+c.getTeach()+"\n"+c.getId());
            tv.setBackgroundColor(getResources().getColor(R.color.classIndex));
            ll.addView(tv);
            pre=c;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
